001    /*
002     * Copyright 2005 Stephen J. McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.tools.tasks;
020    
021    import java.io.File;
022    
023    import net.dpml.library.Resource;
024    
025    import net.dpml.tools.Context;
026    
027    import org.apache.tools.ant.BuildException;
028    import org.apache.tools.ant.Project;
029    
030    /**
031     * Cleanup of generated target directory.
032     *
033     * @author <a href="http://www.dpml.net">The Digital Product Meta Library</a>
034     * @version 1.0.0
035     */
036    public class PackageTask extends GenericTask
037    {
038       /**
039        * Compiles main and test classes resulting in the creation of the 
040        * target/classes/main and target/classes/test directories.
041        */
042        public void execute()
043        {
044            Context context = getContext();
045            Project project = context.getProject();
046            Resource resource = context.getResource();
047            
048            // handle jar file packaging
049            
050            if( resource.isa( "jar" ) )
051            {
052                File base = context.getTargetClassesMainDirectory();
053                final File jar = getJarFile( context );
054                if( base.exists() )
055                {
056                    final JarTask task = new JarTask();
057                    task.setProject( project );
058                    task.setTaskName( "jar" );
059                    task.setSrc( base );
060                    task.setDest( jar );
061                    task.init();
062                    task.execute();
063                }
064            }
065            
066            // handle part production
067            
068            if( resource.isa( "part" ) )
069            {
070                try
071                {
072                    PartTask task = new PartTask();
073                    task.setProject( project );
074                    task.setTaskName( "part" );
075                    task.init();
076                    task.execute();
077                }
078                catch( BuildException e )
079                {
080                    throw e;
081                }
082                catch( Throwable e )
083                {
084                    final String error = 
085                      "Unexpected failure during part externalization.";
086                    throw new BuildException( error, e );
087                }
088            }
089            
090            // handle module export
091            
092            if( resource.isa( "module" ) )
093            {
094                final ModuleTask task = new ModuleTask();
095                task.setProject( project );
096                task.setTaskName( "module" );
097                task.init();
098                task.execute();
099            }
100        }
101        
102        private File getJarFile( Context context )
103        {
104            Project project = context.getProject();
105            File deliverables = context.getTargetDeliverablesDirectory();
106            File jars = new File( deliverables, "jars" );
107            String filename = context.getLayoutFilename( "jar" );
108            return new File( jars, filename );
109        }
110    }